﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Net;

namespace FreeTool
{
    public partial class BatchCopy : Form
    {
        public BatchCopy()
        {
            InitializeComponent();
        }



        //0, 192, 0  绿色
        private void btnSource_Click(object sender, EventArgs e)
        {
            var source = new FolderBrowserDialog();
            source.ShowDialog();
            tbSource.Text = source.SelectedPath.Trim();

            this.BeginInvoke((Delegate)(() =>
            {
                try
                {
                    pbFile.Enabled = true;
                    pbFile.Maximum = 2;
                    pbFile.Minimum = 0;
                    lblMgs.Text = "读取文件信息";
                    lblMgs.ForeColor = Color.White;
                    lblMgs.BackColor = Color.DarkGreen;
                    var ffes = readFolder(tbSource.Text);
                    pbFile.Value = 1;
                    lblMgs.Text = "生成树形";
                    generateTreeViewNode(ffes, tbSource.Text, null);
                    pbFile.Value = 2;
                    lblMgs.Text = "完成";
                    pbFile.Value = 0;
                    pbFile.Maximum = ffes.Count;
                }
                catch (Exception ex)
                {
                    lblMgs.Text = ex.Message;
                    lblMgs.BackColor = Color.DarkRed;
                }
            }));
        }

        List<FolderFileEntity> readFolder(string path)
        {
            try
            {
                if (string.IsNullOrEmpty(path))
                {
                    MessageBox.Show(this, "请选择源文件夹", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return new List<FolderFileEntity>();
                }
                //1.读取所有文件信息
                var files = new List<FolderFileEntity>();
                DirectoryInfo di = new DirectoryInfo(path);
                foreach (var item in di.GetDirectories())
                {
                    var fd = new FolderFileEntity();
                    fd.Name = item.Name;
                    fd.path = item.FullName;
                    fd.Parent = path;
                    fd.IsFolder = true;
                    files.Add(fd);

                    var _fs = readFolder(fd.path);
                    files.AddRange(_fs);
                }

                foreach (var item in di.GetFiles())
                {
                    var f = new FolderFileEntity();
                    f.Name = item.Name;
                    f.path = item.FullName;
                    f.Parent = path;
                    f.IsFolder = false;
                    files.Add(f);
                }

                return files;
            }
            catch (Exception ex)
            {
                throw new Exception("Read:" + ex.Message);
            }

        }

        void generateTreeViewNode(List<FolderFileEntity> ffes, string p, TreeNode? pTN)
        {
            try
            {
                //2.生成树形
                if (ffes.Count <= 0)
                {
                    return;
                }

                var ffes_parents = ffes.FindAll(a => a.Parent == p);
                if (ffes_parents.Count == 0) { return; }
                foreach (var ff in ffes_parents)
                {
                    TreeNode tn = new TreeNode(ff.Name);
                    tn.Text = ff.Name;
                    tn.Tag = ff;
                    tn.Checked = true;
                    //tn.IsExpanded = true;
                    if (pTN != null)
                    {
                        pTN.Nodes.Add(tn);
                    }
                    else
                    {
                        tvSource.Nodes.Add(tn);
                    }

                    generateTreeViewNode(ffes, ff.path, tn);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Build:" + ex.Message);
            }

        }

        private void btnCopy_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(tbTarget.Text))
            {
                MessageBox.Show(this, "请选择目标文件夹", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (tvSource.Nodes.Count <= 0)
            {
                MessageBox.Show(this, "请源目标文件夹，没有文件可以复制", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            this.BeginInvoke(() =>
            {
                lblMgs.Text = "正在复制";
                lblMgs.ForeColor = Color.White;
                lblMgs.BackColor = Color.DarkGreen;

                generateTreeViewNodeTarget(tvSource.Nodes, null);

                lblMgs.Text = "完成";
                lblMgs.ForeColor = Color.White;
                lblMgs.BackColor = Color.DarkGreen;
            });
        }

        void generateTreeViewNodeTarget(TreeNodeCollection tns, TreeNode? pTN)
        {
            try
            {
                //1.生成树形
                foreach (TreeNode item in tns)
                {
                    var ffe = (FolderFileEntity)item.Tag;
                    var pPath = pTN == null ? tbTarget.Text : (string)pTN.Tag;
                    //pPath = pPath.Replace(tbSource.Text, tbTarget.Text);
                    var _path = Path.Combine(pPath, ffe.Name);
                    var _tn = new TreeNode(ffe.Name);
                    _tn.BackColor = Color.DarkGreen;
                    _tn.Checked = true;
                    _tn.ForeColor = Color.White;
                    _tn.Tag = _path;
                    try
                    {
                        if (ffe.IsFolder)
                        {
                            if (!Directory.Exists(_path))
                            {
                                Directory.CreateDirectory(_path);
                            }
                        }
                        else
                        {

                            FileInfo fi = new FileInfo(ffe.path);
                            fi.CopyTo(_path, true);
                            ffe.Name = ffe.Name + " 完成";
                        }
                    }
                    catch (Exception ex1)
                    {
                        ffe.Name = ffe.Name + " 失败:" + ex1.Message;
                        _tn.BackColor = Color.Red;
                        _tn.Checked = false;
                    }
                    _tn.Text = ffe.Name;
                    if (pTN != null)
                    {
                        pTN.Nodes.Add(_tn);
                    }
                    else
                    {
                        tvTarget.Nodes.Add(_tn);
                    }

                    if (ffe.IsFolder && item.Nodes.Count > 0)
                    {
                        generateTreeViewNodeTarget(item.Nodes, _tn);
                    }
                    pbFile.Value++;
                }
            }
            catch (Exception ex)
            {
                throw new Exception("BuildTarget:" + ex.Message);
            }

        }

        private void btnTarget_Click(object sender, EventArgs e)
        {
            var target = new FolderBrowserDialog();
            target.ShowDialog();
            tbTarget.Text = target.SelectedPath.Trim();
        }
    }
}

